package com.leo.train.business.controller.admin;

import com.leo.train.business.dto.DailyTrainQueryDto;
import com.leo.train.business.entity.DailyTrain;
import com.leo.train.business.service.DailyTrainService;
import com.leo.train.business.service.SeatService;
import com.leo.train.business.vo.DailyTrainQueryVo;
import com.leo.train.common.resp.CommonResp;
import com.leo.train.common.resp.PageResult;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/daily-train")
@Slf4j
public class DailyTrainAdminController {
    @Autowired
    private DailyTrainService dailyTrainService;
    @Autowired
    private SeatService seatService;
    /**
     * 新增
     * @param dailyTrain
     * @return
     */
    @PostMapping("/add")
    public CommonResp save(@Valid @RequestBody DailyTrain dailyTrain) {
        dailyTrainService.save(dailyTrain);
        return new CommonResp<>();
    }

    /**
     * 修改
     * @param dailyTrain
     * @return
     */
    @PostMapping("/update")
    public CommonResp update(@Valid @RequestBody DailyTrain dailyTrain) {
        dailyTrainService.save(dailyTrain);
        return new CommonResp<>();
    }

    /**
     * 查询车次列表
     * @param dailyTrainQueryDto
     * @return
     */
    @GetMapping("/list")
    public CommonResp<PageResult<DailyTrainQueryVo>> list(DailyTrainQueryDto dailyTrainQueryDto) {
        return new CommonResp<>(dailyTrainService.list(dailyTrainQueryDto));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/detail")

    public CommonResp<DailyTrainQueryVo> detail(@RequestParam Long id) {
        return new CommonResp<>(dailyTrainService.detail(id));
    }

    /**
     * 删除
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public CommonResp delete(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        dailyTrainService.delete(id);
        return new CommonResp<>();
    }

    /**
     * 查询车次编码所有列表
     */
    @GetMapping("/listAll")
    public CommonResp<List<DailyTrain>> listAll() {
        return new CommonResp<>(dailyTrainService.listAll());
    }

    /**
     * 生成座位
     * @param params
     * @return
     */
    @PostMapping("/generateSeats")
    public CommonResp generateSeats(@RequestBody Map<String, String> params) {
        String trainCode = params.get("code");
        seatService.generateSeats(trainCode);
        log.info("生成座位开始，trainCode: {}", trainCode);
        return new CommonResp<>();
    }

    /**
     * 根据日期生成某日所有车次
     * @param date
     * @return
     */
    @GetMapping("/gen-daily/{date}")
    public CommonResp genDaily(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        dailyTrainService.genDaily(date);
        return new CommonResp<>();
    }

}
